// import './assets/main.css'

import { createApp } from 'vue'

import App from './App.vue'
import './assets/less/index.less'
import router from './router'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import store from './store/index.js'
import './api/mock.js'
import api from './api/api.js'
const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
//挂载api到全局
app.config.globalProperties.$api = api
store.commit('getMenu', router)
//对比跳转的路由
function checkRouter(path) {
  //拿到所有渲染的动态路由 通过过滤比较 如果有值 输出长度 如果没有值 长度就是0
  let hasCheck = router.getRoutes().filter(route => route.path == path).length
  if (hasCheck) {
    return true
  } else {
    return false
  }
  console.log(router.getRoutes());
}
checkRouter()
//路由守卫  首先拿到token 
router.beforeEach((to, from, next) => {
  store.commit('getToken')
  const token = store.state.token
  //判断 如果没有token 同时又不跳向登录页 自动跳向登陆页
  if (!token && to.name !== 'login') {
    next({ name: 'login' })
    //如果有token但是往不存在的路由跳 就自动跳回home页面
  } else if (!checkRouter(to.path)) {
    next({ name: 'home' })
  } else {
    next()
  }
})
app.use(router).use(store)

app.mount('#app')
